﻿#include "connectCameraInterface.h"

ConnectCameraInterface::ConnectCameraInterface()
{

    connectAllCamera();

}

/** @brief 单例函数 */
ConnectCameraInterface& ConnectCameraInterface::I(){
    static ConnectCameraInterface instance;
    return instance;
}


void ConnectCameraInterface::connectCamera(int cameraIdx, QString cameraType, QString ip, QString port, QString username, QString password){

    __LOG_FSTART(QString::number(cameraIdx) + ip + port + username + password);

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    if(cameraIdx != 1 && cameraIdx != 2 && cameraIdx != 4){
        __LOG_ERROR("InvalidCameraIdx");
        __LOG_FEND__;
        QMLSignal::I().emitCameraConnect(false);
        return;
    }

    // 更新数据库内容
    if(ip != ""){
        QString sql = QString("replace into CameraIpInfo values('%1', '%2', '%3', '%4', '%5', '%6')").arg(QString::number(cameraIdx), cameraType, ip, port, username, password);
        query.exec(sql);
    }else{
        QString sql = QString("Select * from CameraIpInfo where Type='%1'").arg(cameraIdx);
        query.exec(sql);
        while(query.next()){
            ip = query.value("Ip").toString();
            port = query.value("Port").toString();
            username = query.value("Username").toString();
            password = query.value("Password").toString();
            cameraType = query.value("CameraType").toString();
        }
        if(ip == ""){
            __LOG_FEND("InvalidIpInfoInDB-ConnectFailure");
            QMLSignal::I().emitCameraConnect(false);
            return;
        }

    }

    QString cameraName;
    if(cameraIdx == 1)  cameraName = "CameraShifting";
    else if(cameraIdx == 2) cameraName = "CameraPractice";
    else if(cameraIdx == 4) cameraName = "CameraPurify";

    CameraManager::I().addCamera(cameraName, ip, port.toInt(), username, password, cameraType);
    bool res = CameraManager::I().connectCamera(cameraName);


    // HIK的连接在这儿显示，JF的摄像头是回调……
    if(cameraType == "HIK"){

        if(res){
            QMLSignal::I().emitCameraConnect(true);
            __LOG_FEND("ConnectSucceed");
        }else{
            QMLSignal::I().emitCameraConnect(false);
            __LOG_FEND("ConnectFailure");
        }
    }
    return;
}

void ConnectCameraInterface::connectAllCamera(){
    connectCamera(1);
    connectCamera(2);
    connectCamera(4);
}

QStringList ConnectCameraInterface::getCameraInfo(int cameraIdx){

    __LOG_FSTART(cameraIdx);

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql = QString("Select * from CameraIpInfo where Type = '%1'").arg(QString::number(cameraIdx));
    query.exec(sql);

    QStringList res;
    res.push_back("HIK");
    res.push_back("");
    res.push_back("");
    res.push_back("");
    res.push_back("");

    while(query.next()){
        res[0] = query.value("CameraType").toString();
        res[1] = query.value("Ip").toString();
        res[2] = query.value("Port").toString();
        res[3] = query.value("Username").toString();
        res[4] = query.value("Password").toString();
        __LOG_FEND(res);
        return res;
    }

    __LOG_FEND(res);
    return res;


}
